package com.leetcode.math;

public class Sqrt {
    public int mySqrt(int x) {
        // 二分法
        int l = 0, r = x, ans = 0;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if ((long)mid * mid <= x) {
                ans = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        // int x = Integer.MAX_VALUE;
        int x = 2147395599;
        int res = new Sqrt().mySqrt(x);
        System.out.println(res);
    }
}
